package com.example.ui_demo_wight;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.ui_demo_wight.adapter.ClassroomBuildingAdapter;
import com.example.ui_demo_wight.pojo.ClassroomBuilding;

import java.util.ArrayList;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);

        //数据（Data）<-->数据适配器(Adapter)<-->界面(Spinner)

        //region 学期下拉框： Spinner加载数据静态数据
        Spinner spSemester = findViewById(R.id.sp_semester);
        spSemester.setSelection(0, true);
        spSemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

        //region 学院下拉框 ：Spinner通过ArrayAdapter加载动态数据
        String[] school = {"===选择学院===","计算机学院","理学院","外语学院"};
        Spinner spSchool = findViewById(R.id.sp_school);
        spSchool.setSelection(0, true);
        ArrayAdapter<String> adapter =
                new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, school);
        spSchool.setAdapter(adapter);
        spSchool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

        //region 年级下拉框：Spinner通过ArrayAdapter加载动态数据，使用自定义下拉列表试图
        String[] grades = {"===选择年级===","大一","大二","大三","大四"};
        Spinner spGrade = findViewById(R.id.sp_grade);
        ArrayAdapter<String> gradeAdapter =
                new ArrayAdapter<>(this, R.layout.my_spinner_item, grades);
        gradeAdapter.setDropDownViewResource(R.layout.my_spinner_item_dropdown);
        spGrade.setAdapter(gradeAdapter);
        spGrade.setSelection(0,true);
        spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

        //region 教学楼下拉框：Spinner通过自定义适配器加载动态数据，使用自定义下拉
        ArrayList<ClassroomBuilding> classroomBuildings = new ArrayList<>();
        classroomBuildings.add(new ClassroomBuilding("===选择教室===","",""));
        classroomBuildings.add(new ClassroomBuilding("文友楼","510","多媒体"));
        classroomBuildings.add(new ClassroomBuilding("文友楼","618","多媒体"));
        classroomBuildings.add(new ClassroomBuilding("香远楼","210","普通"));
        classroomBuildings.add(new ClassroomBuilding("实验楼","602","计算机实验室"));
        Spinner spClassroomBuilding = findViewById(R.id.sp_classroom_building);
        spClassroomBuilding.setAdapter(new ClassroomBuildingAdapter(this,classroomBuildings));
        spClassroomBuilding.setSelection(0,true);
        spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //endregion
    }
}